import jieba 
import random
emotions=['anger','disgust','fear','joy','sadness']
#读取文本文档
with open(r'C:\Users\User\Desktop\code\pycode\lab2\weibo.txt','r',encoding='utf-8') as f:#路径自行修改！！
    text=f.read().split('\n')
text=random.sample(text,500)#抽了500个，节约时间，但量不大可以删除这行代码

#进行加入情感词典的jieba分割
def cut_new(line,emotions):
    text=[]
    line_cut=line.split('\t')#按 \t’分割
    text=line_cut[1]#视情况改索引！！
    #jieba 分割
    text_word=[]
    #将情感词加入jieba的自定义词典
    for i in emotions:  
        jieba.load_userdict("C:\\Users\\User\\Desktop\\code\\pycode\\lab3\\emotion_lexicon\\"+i+".txt")#在这里清务必用双斜线不要用r转义
    #停用词表
    stop_word=[]
    with open(r'C:\Users\User\Desktop\code\pycode\baidu_stopwords.txt',encoding="utf-8") as fs:
        stop_word.append(fs.read().split('\n'))
    #去除停用词
    text=jieba.cut(text)
    for i in text:
        if i not in stop_word:
            text_word.append(i)
    return text_word

#情绪分析函数1
def emo_ana(emotions,way):
    dic_name={}
    for names in emotions:
        with open("C:\\Users\\User\\Desktop\\code\\pycode\\lab3\\emotion_lexicon\\"+names+".txt",
                encoding="utf-8") as f :
            dic_name[names]=f.read().split('\n')#同一个情感类转换为字典
    if way==1:
        def emo_vector(cut_line):
            nonlocal dic_name           
            dic={"anger":0,"disgust":0,"fear":0,"joy":0,"sadness":0}
            for names in emotions:
                for i in cut_line:
                    if i in dic_name[names]:
                        dic[names]+=1#累加次数
            total=sum(dic.values())#总情感词个数
            if total==0:
                return "neutral"
            else:
                vector=[x/total for x in dic.values()]#分量
                return vector
        return emo_vector
    else:
        def emo(cut_line):
            dic={"anger":0,"disgust":0,"fear":0,"joy":0,"sadness":0}
            nonlocal dic_name
            lis=[]
            for values in dic.values():
                lis.append(values)
            
            m=max(lis) 
            ind=[] #索引表
            count=0
            for j in lis:
                if j == m:
                    count+=1
                    ind.append(lis.index(j))
            if count==1:                  
                i = lis.index(max(lis))
            else:
                i=random.choice(ind)
            return emotions[i]
        return emo

#按照每个星期时间段的分割代码
def time_anger(text):#text是按照换行符分割的句子，即三个部分
    emotions=['anger','disgust','fear','joy','sadness']
    time=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    total=[0]*12
    for sen in text:#i是单句
        mon=sen.split('\t')[2].split(' ')[1]
        for i in range(12):
            if time[i]==mon:
                lis_temp=emo_ana(emotions,1)(cut_new(sen,emotions))
                #print(i,lis_temp)
                if lis_temp!="neutral":
                    total[i]+=float(lis_temp[0])#加上每一句的joy分量
    return total

print(time_anger(text))#估计要修改成第二个函数的调用                

             
   
